<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class CollPosition extends Model
{
    protected static $baseImg = 'https://liande.oss-cn-beijing.aliyuncs.com';

    protected $table = 'coll_position';
    //设置主键值
    protected $primaryKey = 'id';
    public $timestamps = false;
    protected $fillable = [
        'cid', 'name', 'grade', 'img1', 'img2', 'pid', 'sort'
    ];

    /**
     * 该分类下的子分类
     */
    public function child()
    {
        return $this->hasMany(get_class($this), 'pid', 'cid');
    }

    /**
     * 该分类的父分类
     */
    public function parent()
    {
        return $this->hasMany(get_class($this), 'cid', 'pid');
    }

    /**
     * $par  $id 当前选项ID
     * 获取当前选项职位自己ID
     * @return array
     */
    public static function CurrPosition($id)
    {
        $currArr = self::find($id)->toArray();
        $arr = array();
        if ($currArr) {
            if ($currArr['grade'] == 1) {
                $arr[] = $currArr['id'];
                //当前二级职位
                $twoPosition = self::where('grade', 2)->where('pid', $currArr['cid'])->orderBy('cid', 'asc')->get();
                $twoPosition = setSort($twoPosition);
                foreach ($twoPosition as $k => $v) {
                    $arr[] = $v['id'];
                    //当前三级职位
                    $threePosition = self::where('grade', 3)->where('pid', $v->cid)->orderBy('cid', 'asc')->get();
                    $threePosition = setSort($threePosition);
                    foreach ($threePosition as $vv) {
                        $arr[] = $vv['id'];
                    }
                }
                array_push($arr, $currArr['id']);
            } else if ($currArr['grade'] == 2) {
                $arr = array_column(self::where('pid', $currArr['cid'])->get()->toArray(), 'id');
                array_push($arr, $currArr['id']);
            } else {
                $arr[] = $currArr['id'];
            };
            return $arr;
        } else {
            return array();
        }
    }

//-----------------------------------------------------------------------------------------------------------------------

    /**
     * 提取一级职位
     * @return array
     */
    public static function getFirstPosition()
    {
        $position = self::where('grade', 1)->orderBy('sort', 'asc')->get();
//        $positionArray = array();
//        foreach ($position as $key => $value) {
//            $positionArray[] = array('value' => $value['name'], 'text' => $value['cid']);
//        }
        return $position;
    }

    /**
     * 提取二级职位
     * @return array
     * $pid:一级职位的id
     */
    public static function getSecondPosition($pid)
    {
        $position = self::where('grade', 2)->where('pid', $pid)->where('status', 1)->orderBy('sort', 'asc')->get();

        $positionArray = array();
        foreach ($position as $key => $value) {
            $positionArray[] = array(
                'id' => $value['id'],
                'value' => $value['name'],
                'text' => $value['cid'],
                'img1' => self::$baseImg . '/' . $value['img1'],
                'img2' => self::$baseImg . '/' . $value['img2'],
                'bg_color' => $value['bg_color']
            );
        }
        return $positionArray;
    }

    /**
     * 提取三级职位
     * @return array
     * $pid:二级职位的id
     */
    public static function getThirdPosition($pid)
    {
        $position = self::where('grade', 3)->where('pid', $pid)->get();
        $position = setSort($position);
        $positionArray = array();
        foreach ($position as $key => $value) {
            $positionArray[] = array('value' => $value['name'], 'text' => $value['cid']);
        }
        return $positionArray;
    }

    /**
     * 提取一级和二级职位//二维
     * @return array
     */
    public static function getAllTwo()
    {
        $allTwoArray = array();
        if (Cache::has('allTwoPosition')) {
            $allTwoArray = Cache::get('allTwoPosition');
        } else {
            $firstPosition = self::where('grade', 1)->get();
            $firstPosition = setSort($firstPosition);
            foreach ($firstPosition as $key => $value) {
                $secondPosition = self::where('grade', 2)->where('pid', $value['cid'])->get();
                $secondPosition = setSort($secondPosition);
                $secondPositionArray = array();
                foreach ($secondPosition as $k => $v) {
                    $secondPositionArray[] = array('value' => $v['name'], 'text' => $v['cid']);
                }
                $allTwoArray[] = array('value' => $value['name'], 'text' => $value['cid'], 'secondPosition' => $secondPositionArray);
            }
            Cache::forever('allTwoPosition', $allTwoArray);
        }
        return $allTwoArray;
    }


    /**
     * 提取一级、二级、三级职位//三维
     * @return array
     */
    public static function getAllThree()
    {
        $allThreeArray = array();
        if (Cache::has('allThreePosition')) {
            $allThreeArray = Cache::get('allThreePosition');
        } else {
            $firstPosition = self::where('grade', 1)->get();
            $firstPosition = setSort($firstPosition);

            foreach ($firstPosition as $key => $value) {
                $secondPosition = self::where('grade', 2)->where('pid', $value['cid'])->get();
                $secondPosition = setSort($secondPosition);

                $secondPositionArray = array();
                foreach ($secondPosition as $k => $v) {
                    $thirdPosition = self::where('grade', 3)->where('pid', $v['cid'])->get();
                    $thirdPosition = setSort($thirdPosition);

                    $thirdPositionArray = array();
                    foreach ($thirdPosition as $third_k => $third_v) {
                        $thirdPositionArray[] = array('value' => $third_v['name'], 'text' => $third_v['cid']);
                    }
                    $secondPositionArray[] = array('value' => $v['name'], 'text' => $v['cid'], 'thirdPosition' => $thirdPositionArray);
                }
                $allThreeArray[] = array('value' => $value['name'], 'text' => $value['cid'], 'secondPosition' => $secondPositionArray);
            }
            Cache::forever('allThreePosition', $allThreeArray);
        }
        return $allThreeArray;
    }

    public static function getOnlyThree()
    {
        if (Cache::has('onlyThreePosition')) {
            return Cache::get('onlyThreePosition');
        } else {
            $arr = self::where('grade', 3)->orderBy('cid')->pluck('name', 'cid')->toArray();
            Cache::forever('onlyThreePosition', $arr);
            return $arr;
        }
    }

    /**
     * 清理本模型中的所有缓存数据
     */
    public static function clearCache()
    {
        Cache::forget('allTwoPosition');
        Cache::forget('allThreePosition');
        Cache::forget('onlyThreePosition');
        return '';
    }

    /**
     * 提取搜索的三级信息
     * $keyword 搜索的值
     */
    public static function getSearchThree($keyword)
    {
        $arr = [];
        //获取三级职位
        $thirdPosition = self::where('grade', 3)->get()->toArray();
        $thirdPosition = setSort($thirdPosition);
        foreach ($thirdPosition as $v) {
            $num = cnsimilar_text($v['name'], $keyword);
            if (cnsimilar_text($v['name'], $keyword) != 0) {
                $arr[] = ['name' => $v['name'], 'cid' => $v['cid'], 'pid' => $v['pid'], 'num' => $num];
            };
        }
        if (count($arr) > 0) {
            $newArr = array();
            for ($j = 0; $j < count($arr); $j++) {
                $newArr[] = $arr[$j]['num'];
            }
            //根据相似度的值倒序排序
            array_multisort($newArr, SORT_DESC, $arr);
            $arr = array_slice($arr, 0, 10);
        }
        return $arr;
    }

    /**
     * $cid:选项编号
     * @return： 选项名称
     */
    public static function getPosName($cid)
    {
        $city = self::where('cid', $cid)->first();
        if ($city) {
            return $city->name;
        }
        return '';
    }

}
